var cokstoreId = $.cookie('cokstoreId');
console.log(cokstoreId);
var companyIdVal,
    bankIdVal;
$("document").ready(function() {
    integralListPaga();
});

$("#startTime").datetimepicker({
    format: "yyyy-mm-dd",
    autoclose: true,
    todayBtn: true,
    todayHighlight: true,
    showMeridian: true,
    pickerPosition: "top-left",
    language: 'zh-CN', //中文，需要引用zh-CN.js包
    startView: 2, //月视图
    minView: 2, //日期时间选择器所能够提供的最精确的时间选择视图

})

function integralListPaga() {
    data = {
        'storeId': cokstoreId
    };
    var pageUrls = serverURL + '/platform/queryStoreById';
    ajax(pageUrls, data, OpendList, 'post', 'json')
}
//数据渲染
function OpendList(res) {
    //console.log(integralList);
    var res = res.data;
    companyIdVal = res.companyId;
    bankIdVal = res.bankId;
    $('input[name="companyName"]').val(res.companyName); //公司名称
    $('#companyProvinceId').text(res.companyProvinceId); //公司所在地
    $('input[name="companyAddress"]').val(res.companyAddress); //公司详细地址
    $('input[name="companyPhone"]').val(res.companyPhone); //公司电话
    $('input[name="companyEmployeeCount"]').val(res.companyEmployeeCount); //员工总数
    $('input[name="companyRegisteredCapital"]').val(res.companyRegisteredCapital); //注册资金
    $('input[name="contactsName"]').val(res.contactsName); //联系人姓名
    $('input[name="contactsPhone"]').val(res.memberMobile); //联系人电话
    $('input[name="contactsEmail"]').val(res.contactsEmail); //联系人邮箱
    $('input[name="businessLicenceNumber"]').val(res.businessLicenceNumber); //营业执照号
    $('#businessLicenceAddress').text(res.businessLicenceAddress); //营业执照所在地
    $('input[name="businessLicenceStart"]').val(res.businessLicenceStart); //营业执照有效期开始
    $('input[name="businessLicenceEnd"]').val(res.businessLicenceEnd); //营业执照有效期结束
    $('input[name="businessSphere"]').val(res.businessSphere); //法定经营范围
    $('input[name="businessLicenceNumberElectronic"]').val(res.businessLicenceNumberElectronic); //营业执照电子版(图片)
    $('#businessLicenceNumberElectronic').attr('src','http://axyhpic.oss-cn-zhangjiakou.aliyuncs.com/'+res.businessLicenceNumberElectronic)
    $('input[name="organizationCode"]').val(res.organizationCode); //组织机构代码
    $('#organizationCodeElectronic').attr('src','http://axyhpic.oss-cn-zhangjiakou.aliyuncs.com/'+res.organizationCodeElectronic); //组织机构代码证电子版(图片)
    $('#generalTaxpayer').attr('src','http://axyhpic.oss-cn-zhangjiakou.aliyuncs.com/'+res.generalTaxpayer); //一般纳税人证明(图片)
    $('input[name="bankAccountName"]').val(res.bankAccountName); //银行开户名
    $('input[name="bankAccountNumber"]').val(res.bankAccountNumber); //公司银行账号
    $('input[name="bankName"]').val(res.bankName); //开户银行支行名称：
    $('input[name="bankCode"]').val(res.bankCode); //支行联行号
    $('#bankLicenceElectronic').attr('src','http://axyhpic.oss-cn-zhangjiakou.aliyuncs.com/'+res.bankLicenceElectronic)
    $('#bankAddress').text(res.bankAddress);
    $('input[name="taxRegistrationCertificate"]').val(res.taxRegistrationCertificate); //税务登记证号
    $('input[name="taxpayerId"]').val(res.taxpayerId); //纳税人识别号
    $('#taxRegistrationCertificateElectronic').attr('src','http://axyhpic.oss-cn-zhangjiakou.aliyuncs.com/'+res.taxRegistrationCertificateElectronic)//税务登记证号电子版(图片)
    // $('input[name="bankCode"]').val(res.bankCode);//支行联行号
    // $('input[name="bankCode"]').val(res.bankCode);//支行联行号
    // $('input[name="bankCode"]').val(res.bankCode);//支行联行号
    // $('input[name="bankCode"]').val(res.bankCode);//支行联行号
}

$('#btnSubmit').click(function() {
    updateStoreCompany();
})

function updateStoreCompany() {
    // for (i in $('.forCompanyProvinceId')){
    //     if($('.forCompanyProvinceId:eq('+i+')').find('option:selected').val() == '0'){
    //         return
    //     }
    // }
    // console.log($('.forCompanyProvinceId:eq(0)').find('option:selected').text());
    var st = $('input[name="businessLicenceStart"]').val();
    var stArr = st.split("-");
    var stDate = new Date(stArr[0], stArr[1], stArr[2])
    var et = $('input[name="businessLicenceEnd"]').val()
    var etArr = et.split("-");
    var etDate = new Date(stArr[0], stArr[1], stArr[2])
    // console.log($('input[name="organizationCode"]').val());
    // console.log($('.forCompanyProvinceId:eq(0)').find('option:selected').val()+','+$('.forCompanyProvinceId:eq(1)').find('option:selected').val()+','+$('.forCompanyProvinceId:eq(2)').find('option:selected').val());
    var forCompanyProvinceIdVal,
        businessLicenceAddressVal,
        bankAddressVal;
    if ($('.cityBox:eq(0)').is(':visible')) {
        forCompanyProvinceIdVal = $('.forCompanyProvinceId:eq(0)').find('option:selected').val();
    };
    if ($('.cityBox:eq(1)').is(':visible')) {
        businessLicenceAddressVal = $('.cityBox:eq(1) select:eq(0)').find('option:selected').text() + ' ' + $('.cityBox:eq(1) select:eq(1)').find('option:selected').text() + ' ' + $('.cityBox:eq(1) select:eq(2)').find('option:selected').text();
    };
    if ($('.cityBox:eq(2)').is(':visible')) {
        bankAddressVal = $('.cityBox:eq(2) select:eq(0)').find('option:selected').text() + ' ' + $('.cityBox:eq(2) select:eq(1)').find('option:selected').text() + ' ' + $('.cityBox:eq(2) select:eq(2)').find('option:selected').text();
    };
    console.log(forCompanyProvinceIdVal);
    // var businessLicenceNumberElectronicPath,//营业执照电子版
    // organizationCodeElectronicPath,//组织机构代码证电子版
    // generalTaxpayerPath,//一般纳税人证明
    // bankLicenceElectronicPath,//开户银行许可证电子版
    // taxRegistrationCertificateElectronicPath;//税务登记证号电子版
    if(st == '' || et == ''){
        data = {
            'companyId': companyIdVal,
            'bankId': bankIdVal,
            'companyName': $('input[name="companyName"]').val(), //公司名称
            'companyProvinceId': forCompanyProvinceIdVal, //公司所在地
            'companyAddress': $('input[name="companyAddress"]').val(), //公司详细地址
            'companyPhone': $('input[name="companyPhone"]').val(), //公司电话
            'companyEmployeeCount': $('input[name="companyEmployeeCount"]').val(), //员工总数
            'companyRegisteredCapital': $('input[name="companyRegisteredCapital"]').val(), //注册资金
            'contactsName': $('input[name="contactsName"]').val(), //联系人姓名
            'contactsPhone': $('input[name="contactsPhone"]').val(), //联系人电话
            'contactsEmail': $('input[name="contactsEmail"]').val(), //联系人邮箱
            'businessLicenceNumber': $('input[name="businessLicenceNumber"]').val(), //营业执照号
            'businessLicenceAddress': businessLicenceAddressVal, //营业执照所在地
            'businessSphere': $('input[name="businessSphere"]').val(), //法定经营范围
            'businessLicenceNumberElectronic':businessLicenceNumberElectronicPath,//营业执照电子版(图片)
            'organizationCode': $('input[name="organizationCode"]').val(), //组织机构代码
             'organizationCodeElectronic':organizationCodeElectronicPath,//组织机构代码证电子版(图片)
             'generalTaxpayer':generalTaxpayerPath,//一般纳税人证明(图片)
            'bankAccountName': $('input[name="bankAccountName"]').val(), //银行开户名
            'bankAccountNumber': $('input[name="bankAccountNumber"]').val(), //公司银行账号
            'bankName': $('input[name="bankName"]').val(), //开户银行支行名称
            'bankCode': $('input[name="bankCode"]').val(), //支行联行号
            'bankAddress': bankAddressVal,
            'bankLicenceElectronic':bankLicenceElectronicPath,
            'taxRegistrationCertificate':$('input[name="taxRegistrationCertificate"]').val(),//税务登记证号
            'taxpayerId':$('input[name="taxpayerId"]').val(),//纳税人识别号
            'taxRegistrationCertificateElectronic':taxRegistrationCertificateElectronicPath//税务登记证号电子版(图片)
        };
    }else{
        data = {
            'companyId': companyIdVal,
            'bankId': bankIdVal,
            'companyName': $('input[name="companyName"]').val(), //公司名称
            'companyProvinceId': forCompanyProvinceIdVal, //公司所在地
            'companyAddress': $('input[name="companyAddress"]').val(), //公司详细地址
            'companyPhone': $('input[name="companyPhone"]').val(), //公司电话
            'companyEmployeeCount': $('input[name="companyEmployeeCount"]').val(), //员工总数
            'companyRegisteredCapital': $('input[name="companyRegisteredCapital"]').val(), //注册资金
            'contactsName': $('input[name="contactsName"]').val(), //联系人姓名
            'contactsPhone': $('input[name="contactsPhone"]').val(), //联系人电话
            'contactsEmail': $('input[name="contactsEmail"]').val(), //联系人邮箱
            'businessLicenceNumber': $('input[name="businessLicenceNumber"]').val(), //营业执照号
            'businessLicenceAddress': businessLicenceAddressVal, //营业执照所在地
            'businessLicenceStart': stDate, //营业执照有效期开始
            'businessLicenceEnd': etDate, //营业执照有效期结束
            'businessSphere': $('input[name="businessSphere"]').val(), //法定经营范围
            'businessLicenceNumberElectronic':businessLicenceNumberElectronicPath,//营业执照电子版(图片)
            'organizationCode': $('input[name="organizationCode"]').val(), //组织机构代码
             'organizationCodeElectronic':organizationCodeElectronicPath,//组织机构代码证电子版(图片)
             'generalTaxpayer':generalTaxpayerPath,//一般纳税人证明(图片)
            'bankAccountName': $('input[name="bankAccountName"]').val(), //银行开户名
            'bankAccountNumber': $('input[name="bankAccountNumber"]').val(), //公司银行账号
            'bankName': $('input[name="bankName"]').val(), //开户银行支行名称
            'bankCode': $('input[name="bankCode"]').val(), //支行联行号
            'bankAddress': bankAddressVal,
            'bankLicenceElectronic':bankLicenceElectronicPath,
            'taxRegistrationCertificate':$('input[name="taxRegistrationCertificate"]').val(),//税务登记证号
            'taxpayerId':$('input[name="taxpayerId"]').val(),//纳税人识别号
            'taxRegistrationCertificateElectronic':taxRegistrationCertificateElectronicPath//税务登记证号电子版(图片)
        };
    }

    console.log(data);
    var pageUrls = serverURL + '/platform/updateStoreCompany';
    var cbk = function() {
        location.reload()
    }
    ajax(pageUrls, data, cbk, 'post', 'json')
}
queryArea();
function queryArea() {
    var areaUrl = serverURL + '/platform/getWmArea';
    data = {};
    var cbk = function(res) {
        console.log(res);
        var areaList = res.data;
        $('.cityBox select:eq(0)').append('<option value="0">请选择</option>')
        for (i in areaList) {
            $('.cityBox select:eq(0)').append('<option value="' + areaList[i].areaId + '">' + areaList[i].areaName + '</option>')
        }
    }
    ajax(areaUrl, data, cbk, 'post', 'json')

    // $('.cityBox select:eq(0)').change(function () {
    //     var areaParentIdVal = $(this).val();
    //     data = {
    //         'areaParentId':areaParentIdVal
    //     };
    //     var cbk = function (res) {
    //         console.log(res);
    //         var areaList = res.data;
    //         $('.cityBox select:eq(1)').append('<option value="0">请选择</option>')
    //         for(i in areaList){
    //             $('.cityBox select:eq(1)').append('<option value="'+areaList[i].areaId+'">'+areaList[i].areaName+'</option>')
    //         }
    //     }
    //     ajax(
    //         areaUrl,
    //         data,
    //         cbk,
    //         'post',
    //         'json'
    //     )
    //
    //     $('.cityBox select:eq(1)').change(function () {
    //         var areaParentIdVal = $(this).val();
    //         data = {
    //             'areaParentId':areaParentIdVal
    //         };
    //         var cbk = function (res) {
    //             console.log(res);
    //             var areaList = res.data;
    //             $('.cityBox select:eq(2)').append('<option value="0">请选择</option>')
    //             for(i in areaList){
    //                 $('.cityBox select:eq(2)').append('<option value="'+areaList[i].areaId+'">'+areaList[i].areaName+'</option>')
    //             }
    //         }
    //         ajax(
    //             areaUrl,
    //             data,
    //             cbk,
    //             'post',
    //             'json'
    //         )
    //     })
    // })

}

$('.cityBox').hide()
$('.fileinputBox').hide()
// $('.btnEditCity').click(function () {
//     $('.addressBox').hide();
//     console.log($('.btnEditCity').index());
//     $('.editBox').hide();
//     $('.cityBox').show()
// })

$('.btnEditCity').each(function(i) {
    $(this).click(function() {
        $('.editBox:eq(' + i + ')').hide();
        $('.cityBox:eq(' + i + ')').show()
        if (i > 0) {
            queryAreaPro(i)
        }
    })
})

$('.btnEditImg').each(function(i) {
    $(this).click(function() {
        $('.imgBox:eq(' + i + ')').hide();
        $('.fileinputBox:eq(' + i + ')').show()
        // if (i > 0) {
        //     queryAreaPro(i)
        // }
    })
});
// 日期选单
function compareDate(phoneStartTime, endTime) {
    return new Date(phoneStartTime).getTime() > new Date(endTime).getTime();
}

function checkStartAndEndTime() {
    if (compareDate($("input[name='businessLicenceStart']").val(), $("input[name='businessLicenceEnd']").val())) {
        $("input[name='businessLicenceStart']").addClass("card-warning");
        $("input[name='businessLicenceStart']").popover('show');

    } else {
        $("input[name='businessLicenceStart']").removeClass("card-warning");
        $("input[name='businessLicenceStart']").popover('hide')
    }
}

$("input[name='businessLicenceStart']").datetimepicker({
    format: "yyyy-mm-dd",
    autoclose: true,
    todayBtn: true,
    todayHighlight: true,
    showMeridian: true,
    pickerPosition: "bottom-left",
    language: 'zh-CN', //中文，需要引用zh-CN.js包
    startView: 2, //月视图
    minView: 2, //日期时间选择器所能够提供的最精确的时间选择视图

}).change(function() {
    checkStartAndEndTime();
});

$("input[name='businessLicenceEnd']").datetimepicker({
    format: "yyyy-mm-dd",
    autoclose: true,
    todayBtn: true,
    todayHighlight: true,
    showMeridian: true,
    pickerPosition: "bottom-left",
    language: 'zh-CN', //中文，需要引用zh-CN.js包
    startView: 2, //月视图
    minView: 2, //日期时间选择器所能够提供的最精确的时间选择视图
}).change(function() {
    checkStartAndEndTime();
});

function queryAreaPro(n) {
    var areaUrl = serverURL + '/platform/getWmArea';
    data = {};
    var cbk = function(res) {
        console.log(res);
        var areaList = res.data;
        $('.cityBox:eq(' + n + ') select:eq(0)').append('<option value="0">请选择</option>')
        for (i in areaList) {
            $('.cityBox:eq(' + n + ') select:eq(0)').append('<option value="' + areaList[i].areaId + '">' + areaList[i].areaName + '</option>')
        }
    }
    ajax(areaUrl, data, cbk, 'post', 'json')

    $('.cityBox:eq(' + n + ') select:eq(0)').change(function() {
        var areaParentIdVal = $(this).val();
        data = {
            'areaParentId': areaParentIdVal
        };
        var cbk = function(res) {
            console.log(res);
            var areaList = res.data;
            $('.cityBox:eq(' + n + ') select:eq(1)').append('<option value="0">请选择</option>')
            for (i in areaList) {
                $('.cityBox:eq(' + n + ') select:eq(1)').append('<option value="' + areaList[i].areaId + '">' + areaList[i].areaName + '</option>')
            }
        }
        ajax(areaUrl, data, cbk, 'post', 'json')

        $('.cityBox:eq(' + n + ') select:eq(1)').change(function() {
            var areaParentIdVal = $(this).val();
            data = {
                'areaParentId': areaParentIdVal
            };
            var cbk = function(res) {
                console.log(res);
                var areaList = res.data;
                $('.cityBox:eq(' + n + ') select:eq(2)').append('<option value="0">请选择</option>')
                for (i in areaList) {
                    $('.cityBox:eq(' + n + ') select:eq(2)').append('<option value="' + areaList[i].areaId + '">' + areaList[i].areaName + '</option>')
                }
            }
            ajax(areaUrl, data, cbk, 'post', 'json')
        })
    })

}
var businessLicenceNumberElectronicPath,//营业执照电子版
organizationCodeElectronicPath,//组织机构代码证电子版
generalTaxpayerPath,//一般纳税人证明
bankLicenceElectronicPath,//开户银行许可证电子版
taxRegistrationCertificateElectronicPath;//税务登记证号电子版
$(".kv-explorer").fileinput({
    'theme': 'fa',
    'maxFileCount':1,
    'uploadUrl': serverURL + '/platform/getOssUpload',
    overwriteInitial: false,
    'uploadExtraData':{
        'imgPath':'store',
    }
});
$(".kv-explorer:eq(0)").on("fileuploaded", function(event, data, previewId, index) {
    console.log(data);
    businessLicenceNumberElectronicPath = data.response.picName;
});
$(".kv-explorer:eq(1)").on("fileuploaded", function(event, data, previewId, index) {
    console.log(data);
    organizationCodeElectronicPath = data.response.picName;
});
$(".kv-explorer:eq(2)").on("fileuploaded", function(event, data, previewId, index) {
    console.log(data);
    generalTaxpayerPath = data.response.picName;
});
$(".kv-explorer:eq(3)").on("fileuploaded", function(event, data, previewId, index) {
    console.log(data);
    bankLicenceElectronicPath = data.response.picName;
});
$(".kv-explorer:eq(4)").on("fileuploaded", function(event, data, previewId, index) {
    console.log(data);
    taxRegistrationCertificateElectronicPath = data.response.picName;
});

//http://axyhpic.oss-cn-zhangjiakou.aliyuncs.com

$('.numInput').each(function() {
    $(this).on('input porpertychange change', function() {
        inputControl(this);
    });
})

//输入控制(只输入数字)
function inputControl(obj) {
    var obj = $(obj)
    var val = obj.val();
    if (val.length == 1) {
        obj.val(val.replace(/[^0-9]/g, ''));
    } else {
        obj.val(val.replace(/\D/g, ''));
    }
    return;
};
